import axios from 'axios'

axios.defaults.withCredentials = true;
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';//配置请求头
import { bus } from '../bus.js'

axios.interceptors.request.use(
    config => {
        //请求头需要带token
        if (window.localStorage.getItem('tokenId')) {
            config.headers.tokenId = window.localStorage.getItem('tokenId');
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
);

// 添加一个响应拦截器
axios.interceptors.response.use(function(response) {
    bus.comm.toastHide();
    if (response.data.result == 'tokenId不合法!登陆过期!' || response.data.exception == 'tokenId不合法!' || response.data.result == 'tokenId不合法!') {
        //token过期重新登录
        localStorage.removeItem('role');
        bus.comm.loginAgain();
    }
    return response;
}, function(error) {
    // Do something with response error
    bus.comm.toastHide();
    bus.comm.tipsShow('timeOut');
    return Promise.reject(error);
});

//基地址 1:国内 0：国外
let base = process.env.VUE_APP_BASE_API
let baseForeign = process.env.VUE_APP_BASE_API2


//通用方法
export const POST = (url, params) => {
    return axios.post(`${base}${url}`, params).then(res => res.data)
}

//通用方法
export const POST2 = (code, url, params) => {
        let domain = base;
        if (code == 1 || code == undefined) {
            domain = base;
        } else {
            domain = baseForeign;
        }
        return new Promise((resolve, reject) => {
            axios.post(`${domain}${url}`, params,
                // 将axios 对象obj数据的post请求转化为formdata格式
                {
                    transformRequest: [function(data) {
                        var str = [];
                        for (var p in data) {
                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(data[p]));
                        }
                        return str.join("&");
                    }],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    timeout: 10000
                }
            ).then(res => {
                resolve(res.data);
            }).catch((error) => {
                reject(error)
            })
        })
    }
    //本地设置地址
let base_local = process.env.VUE_APP_BASE_API_LOCAL
    //通用方法
export const POST3 = (params) => {
    return new Promise((resolve, reject) => {
        axios.post(`${base_local}`, params,
            // 将axios 对象obj数据的post请求转化为formdata格式
            {
                transformRequest: [function(data) {
                    var str = [];
                    for (var p in data) {
                        str.push(encodeURIComponent(p) + "=" + data[p]);
                    }
                    return str.join("&");
                }],
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                timeout: 10000
            }
        ).then(res => {
            resolve(res.data);
        }).catch((error) => {
            reject(error)
        })
    })
}

//本地安规检测
export const POST4 = (params) => {
    return new Promise((resolve, reject) => {
        axios.post(`${base_local}`, params,
            // 将axios 对象obj数据的post请求转化为formdata格式
            {
                transformRequest: [function(data) {
                    var str = [];
                    for (var p in data) {
                        str.push(encodeURIComponent(p) + "=" + data[p]);
                    }
                    return str.join("&");
                }],
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                timeout: 30000
            }
        ).then(res => {
            resolve(res.data);
        }).catch((error) => {
            reject(error)
        })
    })
}


//本地设置地址
let address = process.env.VUE_APP_BASE_API_ADDRESS
export const GET = (params) => {
    return axios.get(`${address}`, {
        params: params
    }).then(res => res.data)
}

export const PUT = (url, params) => {
    return axios.put(`${base}${url}`, params).then(res => res.data)
}

export const DELETE = (url, params) => {
    return axios.delete(`${base}${url}`, {
        params: params
    }).then(res => res.data)
}

export const PATCH = (url, params) => {
    return axios.patch(`${base}${url}`, params).then(res => res.data)
}
export const FILEUPLOAD = (url) => {
        return `${base}${url}`;
    }
    //不带进度条的
export const uploadFile2 = (url, params) => {
        return axios.post(`${base}${url}`, params, { headers: { 'Content-Type': 'Multipart/form-data' } }).then(res => res.data)
    }
    //带进度条的
export const uploadFile3 = (url, params, rogress) => {
    return axios.post(`${base}${url}`, params, {
        headers: { 'Content-Type': 'Multipart/form-data' },
        onUploadProgress: rogress.onUploadProgress,
        timeout: 1000 * 60 * 10
    }).then(res => res.data)
}
export const downloadFile = (url, params) => {
        window.location.href = `${base}${url}?list=${params}`;
    }
    // 图片验证码
export const IMGCODE = (url) => {
    return `${base}${url}`;
}